package com.imoney.payment.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;

import com.imoney.payment.authc.domain.Auth;
import com.imoney.payment.web.common.PathCode;
import com.imoney.payment.web.utils.MenuUtil;

@Controller
@RequestMapping("/portal")
public class IndexController {
	private final static Logger logger = LoggerFactory
			.getLogger(IndexController.class);

	@RequestMapping(value = { "index.htm", "i2.htm" }, method = RequestMethod.GET)
	public String mainpage(final Model model, final HttpServletRequest request) {
		MenuUtil.getMenuItem(0, 0, 0);

		return PathCode.INDEX;
	}

	@RequestMapping(value = "/fileImport", method = RequestMethod.GET)
	public String fileImport(HttpServletRequest request,
			HttpServletResponse response) {

		System.out.println(request.isAsyncSupported());

		Auth menu1 = new Auth();
		menu1.setName("我的e支付");
		menu1.setUrl("/mypay");

		Auth menu2 = new Auth();
		menu2.setName("账户管理");
		menu2.setUrl("/act");

		Auth menu3 = new Auth();
		menu3.setName("交易记录");
		menu3.setUrl("/trade");

		Auth menu4 = new Auth();
		menu4.setName("银行");
		menu4.setUrl("/bank");

		Auth menu5 = new Auth();
		menu5.setName("企业");
		menu5.setUrl("/merch");

		List<Auth> items = new ArrayList<Auth>();
		items.add(menu1);
		items.add(menu2);
		items.add(menu3);
		items.add(menu4);
		items.add(menu5);

		request.getSession().setAttribute("items", items);
		System.out.print(request.getSession().getAttribute("items"));
		System.out.println(request.getServletContext().getRealPath(""));

		return PathCode.INDEX;
	}

	@RequestMapping(value = "/addpage", method = RequestMethod.POST)
	public String addpage(final Model model) {

		System.out.println("iklslldkkd");

		Callable<String> callable = new Callable<String>() {
			@Override
			public String call() throws Exception {
				System.out.println("1111111");
				Thread.sleep(1900);
				System.out.println("12222");
				return "myhome/editfee";
			}
		};

		Callable<String> callable2 = new Callable<String>() {
			@Override
			public String call() throws Exception {
				System.out.println("22222");
				Thread.sleep(300);
				System.out.println("222244442");
				return "myhome/editfee";
			}
		};

		try {
			System.out.println(callable.call() + "  --- " + callable2.call());

			return callable.call();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}

	@RequestMapping(value = "/setting.htm")
	// @ResponseBody
	public Callable<String> setting() {

		return new Callable<String>() {

			@Override
			public String call() throws Exception {

				return "myhome/seting";
			}
		};
	}

	@RequestMapping(value = "/find")
	public DeferredResult<Object> find() {
		DeferredResult<Object> deferredResult = new DeferredResult<Object>();

		// add deferredResult to a Queue or a map

		// in some other thread ...
		// set the return value on the deferedResult

		deferredResult.setResult("");

		return deferredResult;
	}

	@RequestMapping(value = "/find2")
	public Callable<ResponseEntity<String>> findData() {

		return new Callable<ResponseEntity<String>>() {

			@Override
			public ResponseEntity<String> call() throws Exception {

				return null;
			}
		};
	}

}
